package com.ljw.yuanli;

import org.junit.Test;
import org.mvel2.MVEL;
import org.mvel2.integration.VariableResolverFactory;
import org.mvel2.integration.impl.MapVariableResolverFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lijunwei
 * @date 2025/5/21
 */
public class YuanLiDemo {

    /**
     * 底层原理
     */
    @Test
    public void executeExpression0And() {

        Map contextMap = new HashMap();
        contextMap.put("reformResult", 1);
        contextMap.put("ruleCreateShop", 1);
        contextMap.put("sameSeasonTechPunishmentNum", 0);

        contextMap.put("totalData", 1);

        VariableResolverFactory functionFactory = new MapVariableResolverFactory(contextMap);

        String expression = "reformResult==0 &&ruleCreateShop==1 &&sameSeasonTechPunishmentNum<21 && (totalData==null" +
                " || totalData==1)";

        Boolean result = (Boolean) MVEL.eval(expression, functionFactory);
        System.out.println(result);


        Serializable compileExpression = MVEL.compileExpression(expression);
        result = (Boolean) MVEL.executeExpression(compileExpression, new HashMap(), functionFactory);
        System.out.print(result);
    }


    @Test
    public void executeExpression1() {

        Map contextMap = new HashMap();
        contextMap.put("reformResult", 1);
        contextMap.put("ruleCreateShop", 1);
        contextMap.put("sameSeasonTechPunishmentNum", 0);

        contextMap.put("totalData", 1);

        VariableResolverFactory functionFactory = new MapVariableResolverFactory(contextMap);

        String expression = "true";

        Boolean result = (Boolean) MVEL.eval(expression, functionFactory);
        System.out.println("result:" + result);


        Serializable compileExpression = MVEL.compileExpression(expression);
        Boolean result1 = (Boolean) MVEL.executeExpression(compileExpression, new HashMap(), functionFactory);
        System.out.println("result1:" + result1);
    }

    @Test
    public void executeExpression2Or() {
        String expression = "a > 5 || b < 10";  // 使用 OR 运算符的表达式

        // 编译表达式
        Object compiledExpression = MVEL.compileExpression(expression);

        // 创建一个变量映射表
        Map<String, Object> variablesMap = new HashMap<>();
        variablesMap.put("a", 3);  // 设置 a = 3
        variablesMap.put("b", 8);  // 设置 b = 8

        // 评估表达式
        Boolean result = (Boolean) MVEL.executeExpression(compiledExpression, variablesMap);

        // 输出结果
        System.out.println("Result: " + result);  // 输出 false
    }

    @Test
    public void executeExpression3OrAND() {
        String expression = "(a > 5 && b < 10) || (c == 20 && d != 15)";  // 使用 AND 和 OR 运算符的表达式

        // 编译表达式
        Object compiledExpression = MVEL.compileExpression(expression);

        // 创建一个变量映射表
        Map<String, Object> variables = new HashMap<>();
        variables.put("a", 6);  // 设置 a = 6
        variables.put("b", 8);  // 设置 b = 8
        variables.put("c", 20); // 设置 c = 20
        variables.put("d", 10); // 设置 d = 10

        // 评估表达式
        Boolean result = (Boolean) MVEL.executeExpression(compiledExpression, variables);

        // 输出结果
        System.out.println("Result: " + result);  // 输出 true
    }
}
